package org.txazo.blog.commons.parse;

import java.io.*;

public class CssParser {

	public static void main(String[] args) throws Exception {
		readCss(new File(CssParser.class.getResource("").getPath()
				.replaceFirst("bin", "src")
				+ "source/parse.css"));
	}

	public static void readCss(File file) throws Exception {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					file), "UTF-8"));
			String temp = null;
			System.out.print("<span class=\"css-pun\">");
			while ((temp = br.readLine()) != null) {
				parseCss(temp);
			}
			System.out.print("</span>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void parseCss(String line) {
		if (line.equals("")) {
			// 空行
		} else {
			// 非空行
			// 补全空格
			int i = 0;
			int length = line.length();
			for (; i < length; i++) {
				if (!(line.charAt(i) == 9)) {
					break;
				}
			}
			i *= 4;
			for (int j = 0; j < i; j++) {
				System.out.print(" ");
			}
			// 去掉前后空格
			String trim = line.trim();
			if (trim.startsWith("@charset") || trim.startsWith("@CHARSET")) {
				// 字符集
				System.out.print("<span class=\"css-lit\">"
						+ trim.substring(0, 8) + "</span>");
				System.out.print("<span class=\"css-val\">"
						+ trim.substring(8, trim.length() - 1) + "</span>;");
			} else if (trim.equals("}")) {
				System.out.print("}");
			} else if (trim.endsWith("{")) {
				selector(trim.substring(0, trim.length() - 1).trim());
				System.out.print("{");
			} else {
				String[] attr = trim.split(":");
				System.out.print("<span class=\"css-pro\">" + attr[0]
						+ "</span>:");
				String[] dot = attr[1].split(",");
				for (int k = 0; k < dot.length - 1; k++) {
					System.out.print("<span class=\"css-val\">" + dot[k]
							+ "</span>,");
				}
				System.out.print("<span class=\"css-val\">"
						+ dot[dot.length - 1].substring(0,
								dot[dot.length - 1].length() - 1) + "</span>;");
			}
		}
		System.out.println("");
	}

	public static void selector(String selects) {
		String[] data = selects.split(" ");
		for (String select : data) {
			String[] data1 = select.split(",");
			for (int i = 0; i < data1.length - 1; i++) {
				dot(data1[i]);
				System.out.print(",");
			}
			dot(data1[data1.length - 1]);
			System.out.print(" ");
		}
	}

	public static void dot(String dot) {
		if (dot.contains(":")) {
			int index = dot.indexOf(":");
			diffSelector(dot.substring(0, index));
			System.out.print("<span class=\"css-pse\">"
					+ dot.substring(index, dot.length()) + "</span>");
		} else {
			diffSelector(dot);
		}
	}

	public static void diffSelector(String select) {
		char c = select.charAt(0);
		if (c == '#') {
			System.out.print("<span class=\"css-ids\">" + select + "</span>");
		} else if (c == '.') {
			System.out.print("<span class=\"css-cla\">" + select + "</span>");
		} else {
			System.out.print("<span class=\"css-sel\">" + select + "</span>");
		}
	}

}
